package com.mbl.base.modular.config.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mbl.base.core.common.constant.MblConstantFactory;
import com.mbl.base.core.common.controller.BaseController;
import com.mbl.base.core.common.dto.MblResult;
import com.mbl.base.modular.config.entity.Dict;
import com.mbl.base.modular.config.service.IDictService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 配置字典表 前端控制器
 * </p>
 *
 * @author lwj
 * @since 2020-03-08
 */
@RestController
@RequestMapping("/config/dict")
public class DictController extends BaseController<Dict, IDictService> {

    /**
     * 根据code查询类型列表
     * @param code
     * @return
     */
    @GetMapping("/listByCode")
    public MblResult listByCode(String code) {
        final List<Dict> dictListByCode = MblConstantFactory.me().getDictListByCode(code);
        return MblResult.success(dictListByCode);
    }

    @Override
    public void machiningListResult(IPage<Dict> page1) {
        final List<Dict> records = page1.getRecords();
        records.forEach(dict -> {
            final List<Dict> dict_pid = getService().query().eq("dict_pid", dict.getId()).list();
            dict.setChildren(dict_pid);
        });
    }

    @Override
    public void machiningList(QueryWrapper<Dict> queryWrapper, String condition) {
        queryWrapper.eq("dict_pid", 0);
        super.machiningList(queryWrapper, condition);
    }
}

